import React, { Component } from 'react';
import {
    View,
    Button,
    StyleSheet,
    AsyncStorage
} from 'react-native';
import BaseComponentF from './BaseComponentF';

const STORAGE_KEY = 'key_save';
class StorageDemo extends Component {
    constructor(props) {
        super(props);
    }
    render() {
        return (
            <View style={Style.constainer}>
                <Button title='save' onPress={() => { this.doSave() }} />
                <Button title='get' onPress={() => { this.doGet() }} />
            </View>
        );
    }

    doSave() {
        let data = { username: 'java', age: 22 };
        AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(data)).then((result) => {
            console.log(`save success:${result}`);
            alert('save success');
        }).catch((error) => {
            console.log(error);
        });
    }

    doGet() {
        AsyncStorage.getItem(STORAGE_KEY).then((result) => {
            console.log(result);
            alert(`success:${result}`);
        }).catch((error) => {
            console.log(error);
        })
    }
}

const Style = StyleSheet.create({
    constainer: {

    }
});

export default BaseComponentF(StorageDemo);